import {IResultOr} from '@/api/interface'
import {userSignApi,userLoginApi} from '@/api/login'
import {getCurrentInstance} from 'vue'
import {useStore} from '@/store'
import {useRoute} from "vue-router";
interface IRuleForm{//针对表单对象定义的interface
    mobile:string,
    password:string
}
export default function useFormProperties(router:any,params:any){
    const {proxy}:any = getCurrentInstance()
    const store = useStore()
    const route = useRoute()
    //注册接口
    function userSign(params:IRuleForm){
        userSignApi(params).then((res:IResultOr)=>{
            const {success,message} = res
            if(success){
                proxy.$message.success(message)
            }else{
                proxy.$message.error(message)
            }
        })
    }
//登录接口
    function userLogin(params:IRuleForm){
        userLoginApi(params).then((res:IResultOr)=>{
            const {success,message,result} = res
            if(success){
                const {status,userId} = result
                localStorage.setItem('userId',userId)
                store.commit('setUserStatus',status)
                const {redirect}:any = route.query
                router.push({path:redirect||'/home'})
                // window.location.href = '/home'//这个方法会刷新当前页面所以可以 但还是希望使用路由来进行跳转
                proxy.$message.success(message)
            }else{
                proxy.$message.error(message)
            }
        })
    }
    return{
        userSign,
        userLogin
    }
}
